//
//  SceneKPViewVC.m
//  SelfDriving
//
//  Created by apple on 16/3/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SceneKPViewVC.h"
#import "BusinessService+FJ.h"
#import "KPDViewController.h"
#import "FriendDetailsMainVC.h"
#import "LoginResponseModel.h"
#import "XLSwitchView.h"
#import "KpListViewController.h"

//其中在sceneKPViewVC中在加入了XLSwitchView(其中有kplistViewController)
@interface SceneKPViewVC ()<XLSwitchViewDelegate,XhwKpListVCDelegate>
//@property (weak, nonatomic) IBOutlet BaseTable *table;

@property (nonatomic, strong)XLSwitchView *switchView;

//xhw12月28号加:点赞后外面的赞数自动更新下
@property (nonatomic, strong)KpListViewController *xhwKPListVC; //哪个kplist应该刷新

@end
@implementation SceneKPViewVC


-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"initdataren" object:nil];
    
    
    [self.delegate refreshData];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"xhwRefreshListData" object:nil];
    
    
}

- (void)xhwRefreshData {
    
}


-(void)viewDidLoad{
    [super viewDidLoad];
    

//    [self.switchView createUserInterface];
    
    [self.view addSubview:self.switchView];
    [self.view bringSubviewToFront:self.switchView];
    
//    data = [NSMutableArray array];
//    
////    [self initSortView];
//    _table.bdelegate = self;
////    [btnArr mas_distributeViewsAlongAxis: 1 withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
//    
}
-(void)ScrollAction:(NSInteger)index{
//    indexs = (int)index ;
    
}
-(NSInteger)numberOfItemsInSwitchView:(XLSwitchView *)switchView{
    return 3;
}
-(UIViewController *)switchView:(XLSwitchView *)switchView withViewOfItem:(NSInteger)index{ //switchView的一个delegate方法，有几个item就走几次
    KpListViewController *vc = [[KpListViewController alloc]init];
    self.delegate = vc;
    if (index == 0) {
        
        vc.kptype = 4;
        vc.title = @"最新";
        
        
    }
    
    if (index == 1){
        
        vc.kptype = 1;
        vc.title = @"附近";
        
    }
    if (index == 2){
        
        vc.kptype = 5;
        vc.title = @"最火";
        
    }
//    UIViewController *vc = [UIViewController new];
//    vc.title = [NSString stringWithFormat:@"%d",index];
    vc.view.backgroundColor = UIColorMake(index * 100, index * 1, index * 1);
    vc.nav = self.navigationController;
    return vc;
    
}




#pragma mark -- 懒加载
-(XLSwitchView *)switchView{ //用到才加载(这里是在viewDidLoad里面用到self.switchView)
    if (!_switchView) {
        _switchView = [[XLSwitchView alloc]initWithFrame:XYWH(0,64,SCREEN_WIDTH,SCREEN_HEIGHT - 64)];
        _switchView.backgroundColor = UIColorClear;
        _switchView.delegate = self;
        _switchView.topWidth = 44;
        _switchView.btnWidth = SCREEN_WIDTH / 3;
        _switchView.btncolor = UIColorGreen;
        _switchView.inecolor = UIColorGreen;
        CGRect r = _switchView.topScrollView.frame;
        r.size.height = 44;
        _switchView.topScrollView.frame = r;
        
        CGRect s =  _switchView.contentScrollView.frame;
        s.origin.y = 44;
        s.size.height = SCRE_HEIGHT- 64 - 44;
        _switchView.contentScrollView.frame = s;
        [_switchView createUserInterface];
        
        
    }
    return _switchView;
}

/*
 -(void)scollTop:(NSNotification *)not{
 if (data.count!=0) {
 [_table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
 }
 }
 
 -(void)initSortView{
 UIView *headView = View.xywh(0,0,SCREEN_WIDTH,40).bgColor(@"white");
 
 
 NSArray *btnName = @[@"最新",@"附近",@"最热"];
 btnArr = [NSMutableArray array];
 int i = 1;
 for (NSString *name in btnName) {
 QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeSystem];
 [btn setTitle:name forState:UIControlStateNormal];
 [btn setTitleColor:UIColorBlack forState:UIControlStateNormal];
 [btn setTitleColor:UIColorGreen forState:UIControlStateSelected];
 btn.tag = 100 + i++;
 [btn setBackgroundImage:[UIImage qmui_imageWithStrokeColor:UIColorGreen size:WH(SCREEN_WIDTH/3.0,headView.h) lineWidth:2 borderPosition:QMUIImageBorderPositionBottom] forState:UIControlStateSelected];
 [btn addTarget:self action:@selector(sortBtnAction:) forControlEvents:UIControlEventTouchUpInside];
 
 [headView addSubview:btn];
 [btnArr addObject:btn];
 }
 btnArr[0].selected = YES;
 
 
 [btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
 make.height.equalTo(headView.mas_height);
 }];
 //    [btnArr mas_distributeViewsAlongAxis:1 withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
 [btnArr mas_distributeViewsAlongAxis:0 withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
 
 
 _table.tableHeaderView = headView;
 
 }
 
 #pragma mark -- 排序
 -(void)sortBtnAction:(QMUIButton *)sender {
 for (QMUIButton *btn in btnArr) {
 btn.selected = btn.tag == sender.tag;
 }
 }
 
 -(void)initData:(int)index{
 _table.backgroundColor = colorBG;
 NSMutableDictionary *dic = [UserDefaults readUserDefaultObjectValueForKey:@"userLocation"];
 [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
 [dic setObject:[NSNumber numberWithInt:_kptype==0?1:_kptype] forKey:@"type"];
 if (_kptype == 2) {
 [dic setObject:[UserDefaults readUserDefaultObjectValueForKey:@"wifiName"] forKey:@"wifi_ssid"];
 self.navigationItem.title = @"同一WIFI快拍";
 }else if(_kptype == 3){
 self.navigationItem.title = @"附近快拍";
 }
 
 [[BusinessService shareInstance]kuaipai_list:dic success:^(id result) {
 data = [_table result:result olddata:data index:index];
 if (notlabel) {
 [notlabel removeFromSuperview];
 }
 notlabel = (UILabel *)[_table notData];
 if (data.count == 0) {
 [self.view addSubview:notlabel];
 }
 [_table reloadData];
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 }
 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 return data.count;
 }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 return 1;
 }
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
 return cell.frame.size.height;
 }
 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
 return 0;
 }
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
 //    cell.backgroundColor = colorBG;
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 DTModel *model = [data objectAtIndex:indexPath.section];
 UIView *bgview= [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCRE_WIDTH, 300)];
 //基础UI
 int b = [_table Basics:cell dtmodel:model bgview:bgview];
 //个人头像按钮
 UIButton *headbtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 45, 45)];
 headbtn.tag = indexPath.section;
 [headbtn addTarget:self action:@selector(toIndividualAction:) forControlEvents:UIControlEventTouchUpInside];
 [cell.contentView addSubview:headbtn];
 int file = 0;
 
 
 
 //附件
 if (model.img!=nil) {
 //        model.img = model.headsmall;
 file = [_table Attachment:cell dtmodel:model bgview:bgview];
 NSString *url = model.img;
 if ([url trim].length !=0) {
 url = [url substringFromIndex:url.length-3];
 }
 
 
 
 if(model.video.length!=0){
 UIView *bgviews = [[UIView alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 70, 55, 60, 20)];
 bgviews.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.3);
 bgviews.layer.masksToBounds = YES;
 bgviews.layer.borderWidth = 1;
 bgviews.layer.borderColor = UIColorWhite.CGColor;
 bgviews.layer.cornerRadius = 10;
 
 
 [bgview addSubview:bgviews];
 
 
 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 20)];
 lable.text = @"现场";
 lable.layer.cornerRadius = 10;
 lable.clipsToBounds = YES;
 
 lable.textAlignment = NSTextAlignmentCenter;
 lable.textColor = [UIColor whiteColor];
 
 lable.font = [UIFont systemFontOfSize:15];
 [bgviews addSubview:lable];
 //附件按钮
 BaseButton *headbtn = [[BaseButton alloc]initWithFrame:CGRectMake(0,45, SCRE_WIDTH, file)];
 headbtn.tag = indexPath.section;
 [headbtn setImage:[UIImage imageNamed:@"图标---视频播放"] forState:UIControlStateNormal];
 headbtn.index = indexPath.section;
 [headbtn addTarget:self action:@selector(btntoKPDViewController:) forControlEvents:UIControlEventTouchUpInside];
 [bgview addSubview:headbtn];
 }else{
 UIView *bgviews = [[UIView alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 70, 55, 60, 20)];
 bgviews.backgroundColor = UIColorBlack;
 bgviews.alpha = 0.3;
 bgviews.layer.cornerRadius = 10;
 bgviews.layer.masksToBounds = YES;
 bgviews.layer.borderWidth = 1;
 
 bgviews.layer.borderColor = UIColorWhite.CGColor;
 
 [bgview addSubview:bgviews];
 
 
 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 20)];
 lable.text = @"快拍";
 lable.layer.cornerRadius = 10;
 lable.clipsToBounds = YES;
 
 lable.textAlignment = NSTextAlignmentCenter;
 lable.textColor = [UIColor whiteColor];
 lable.font = [UIFont systemFontOfSize:15];
 [bgviews addSubview:lable];
 }
 }
 //内容
 MLEmojiLabel * _emojiLabel = [MLEmojiLabel new];
 _emojiLabel.numberOfLines = 0;
 _emojiLabel.font = [UIFont systemFontOfSize:15.0f];
 _emojiLabel.backgroundColor = [UIColor clearColor];
 _emojiLabel.lineBreakMode = NSLineBreakByWordWrapping;
 _emojiLabel.textColor = [UIColor blackColor];
 
 _emojiLabel.textInsets = UIEdgeInsetsMake(0, 5, 0, 5);
 
 _emojiLabel.isNeedAtAndPoundSign = YES;
 _emojiLabel.disableEmoji = NO;
 _emojiLabel.disableThreeCommon = YES;
 //    _emojiLabel.lineSpacing = 3.0f;
 
 _emojiLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
 _emojiLabel.customEmojiRegex = @"\\[[emoji_[0-9]{1,3}]+\\]";
 _emojiLabel.customEmojiPlistName = @"expressionImage_custom.plist";
 [_emojiLabel setText:model.title];
 
 CGRect g = _emojiLabel.frame;
 g.size.height = ([_emojiLabel preferredSizeWithMaxWidth:bgview.frame.size.width].height+10.0f*2);
 g.origin.y =  b+file;
 g.size.width = SCRE_WIDTH-10;
 g.origin.x = 0;
 _emojiLabel.frame = g;
 [bgview addSubview:_emojiLabel];
 int conth = _emojiLabel.frame.size.height;
 //内容下线
 UILabel *ine = [[UILabel alloc]initWithFrame:CGRectMake(10, b+file+conth,SCRE_WIDTH-40,0.5)];
 ine.backgroundColor = colorBG;
 [bgview addSubview:ine];
 
 
 //底部
 int bottom = b+file+conth;
 UIButton *zanbtn = [[UIButton alloc]initWithFrame:CGRectMake(10, bottom, 120, 30)];
 //    [zanbtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
 [zanbtn setTitleColor:colorGrey forState:UIControlStateNormal];
 zanbtn.titleLabel.font = [UIFont systemFontOfSize:13];
 [zanbtn setTitle:[NSString stringWithFormat:@"❤︎%@次赞",model.zancount] forState:UIControlStateNormal];
 zanbtn.tag = indexPath.section;
 [zanbtn addTarget:self action:@selector(praiseAction:) forControlEvents:UIControlEventTouchUpInside];
 
 
 [zanbtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
 
 [bgview addSubview:zanbtn];
 
 
 
 
 
 CGRect bg = bgview.frame;
 bg.size.height = bottom + 30;
 bgview.frame = bg;
 
 CGRect cl =  cell.frame;
 cl.size.height = bottom + 50;
 cell.frame = cl;
 return cell;
 }
 -(void)btntoKPDViewController:(BaseButton *)btn{
 DTModel *model = [data objectAtIndex:btn.index];
 NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
 [dic setObject:model.id forKey:@"id"];
 [SVProgressHUD showWithStatus:@"加载中..."];
 [[BusinessService shareInstance]kuaipai_detail:dic success:^(id result) {
 user = result;
 [[BusinessService shareInstance]enterGroup:dic success:^(id result) {
 [SVProgressHUD dismiss];
 models = result;
 UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
 bundle:[NSBundle mainBundle]];
 KPDViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"KPDViewController"];
 searchResultDetailController.model = models;
 searchResultDetailController.user = user;
 searchResultDetailController.ary = data;
 searchResultDetailController.notify = not;
 searchResultDetailController.index = btn.index;
 [self.navigationController pushViewController:searchResultDetailController animated:YES];
 
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 
 }
 
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initdata:) name:@"initdataren" object:nil];
 
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
 DTModel *model = [data objectAtIndex:indexPath.section];
 NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
 [dic setObject:model.id forKey:@"id"];
 [SVProgressHUD showWithStatus:@"加载中..."];
 [[BusinessService shareInstance]kuaipai_detail:dic success:^(id result) {
 user = result;
 [[BusinessService shareInstance]enterGroup:dic success:^(id result) {
 [SVProgressHUD dismiss];
 models = result;
 UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
 bundle:[NSBundle mainBundle]];
 KPDViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"KPDViewController"];
 searchResultDetailController.model = models;
 searchResultDetailController.user = user;
 searchResultDetailController.ary = data;
 searchResultDetailController.notify = not;
 searchResultDetailController.index = indexPath.section;
 [self.navigationController pushViewController:searchResultDetailController animated:YES];
 
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 }
 -(void)initdata:(NSNotification *)sender{
 not = sender.object;
 }
 #pragma mark   赞
 - (IBAction)praiseAction:(UIButton *)sender {
 //    inputView.hidden = NO;
 //    [inputView removeFromSuperview];
 //    inputView = [[KYExpressionInputView alloc] init];
 //    inputView.frame = CGRectMake(0, SCRE_HEIGHT-inputView.frame.size.height+5, SCRE_WIDTH, inputView.frame.size.height);
 //    inputView.toolbarColor = [UIColor whiteColor];
 //    inputView.backgroundColor = [UIColor whiteColor];
 //    inputView.tag = sender.tag;
 //    inputView.toolbar.hidden = YES;
 //    [inputView setToolbarSendButtonHidden:YES animated:NO];
 //    [inputView addToolbarItemWithImage:nil title:nil items:[AppDelegate shareDelegate].emojiItems row:KYUIntegerOrientationMake(4, 5) column:KYUIntegerOrientationMake(8, 14) itemSize:KYSizeOrientationMake(CGSizeMake(36, 36), CGSizeMake(36, 36)) itemSpacing:KYFloatOrientationMake(6, 8)];
 //    inputView.delegate = self;
 //    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:inputView];
 
 DTModel *msg = [data objectAtIndex:sender.tag];
 NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
 if ([msg.iszan intValue]==0) {
 [dic setObject:@"1" forKey:@"type"];
 }else{
 [dic setObject:@"0" forKey:@"type"];
 }
 [dic setObject:msg.id forKey:@"id"];
 [dic setObject:@"emoji_52" forKey:@"content"];
 [[BusinessService shareInstance]praise:dic success:^(id result) {
 if ([msg.iszan intValue]==0) {
 msg.zancount = [NSString stringWithFormat:@"%d",[msg.zancount intValue]+1];
 }else{
 msg.zancount = [NSString stringWithFormat:@"%d",[msg.zancount intValue]-1];
 }
 if ([msg.iszan intValue]==0) {
 msg.iszan = @"1";
 }else{
 msg.iszan = @"0";
 }
 [data removeObjectAtIndex:inputView.tag];
 [data insertObject:msg atIndex:inputView.tag];
 [_table reloadData];
 [inputView removeFromSuperview];
 
 } failure:^(id result) {
 [SVProgressHUD showErrorWithStatus:result];
 }];
 }
 - (void)inputView:(KYExpressionInputView *)inputView didSelectExpression:(id<KYExpressionData>)expression {
 //    DTModel *msg = [data objectAtIndex:inputView.tag];
 //    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
 //    if ([msg.iszan intValue]==0) {
 //        [dic setObject:@"1" forKey:@"type"];
 //    }else{
 //        [dic setObject:@"0" forKey:@"type"];
 //    }
 //    [dic setObject:msg.id forKey:@"id"];
 //    [dic setObject:@"emoji_52" forKey:@"content"];
 //    [[BusinessService shareInstance]praise:dic success:^(id result) {
 //        if ([msg.iszan intValue]==0) {
 //            msg.zancount = [NSString stringWithFormat:@"%d",[msg.zancount intValue]+1];
 //        }else{
 //            msg.zancount = [NSString stringWithFormat:@"%d",[msg.zancount intValue]-1];
 //        }
 //        if ([msg.iszan intValue]==0) {
 //            msg.iszan = @"1";
 //        }else{
 //            msg.iszan = @"0";
 //        }
 //        [data removeObjectAtIndex:inputView.tag];
 //        [data insertObject:msg atIndex:inputView.tag];
 //        [_table reloadData];
 //        [inputView removeFromSuperview];
 //
 //    } failure:^(id result) {
 //        [SVProgressHUD showErrorWithStatus:result];
 //    }];
 }
 #pragma mark 个人信息
 - (IBAction)toIndividualAction:(UIButton *)sender {
 DTModel *model = [data objectAtIndex:sender.tag];
 
 UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
 bundle:[NSBundle mainBundle]];
 FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
 searchResultDetailController.model = model;
 [self.navigationController pushViewController:searchResultDetailController animated:YES];
 
 
 }
 */
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



@end
